import { Component } from '@angular/core'
import { NavController } from 'ionic-angular/navigation/nav-controller';
import { Api } from '../../../../providers/api-service';
import { NavParams } from 'ionic-angular/navigation/nav-params';
import { HaokurBasePage } from '../../haokur-base.page';
import { StudyListData } from '../../../../mocks/study-list.mock';
import { StudyDetailPage } from '../study-detail/study-detai';

@Component({
  selector: 'page-study-list',
  templateUrl: 'study-list.html'
})

export class StudyListPage extends HaokurBasePage {

  studyList = []
  keywordInput = '';
  typeActive = 0; // tab类型
  classTypeId = '1'; // 分类类型

  classTypeConfig = [
    {
      title: '健身',
      classType: 10,
      sub: [
        { name: '健身', id: 10 },
      ]
    },
    {
      title: '健心',
      classType: 20,
      sub: [
        { name: '健心', id: 20 },
      ]
    },
    {
      title: '健脑',
      classType: 30,
      sub: [
        { name: '健脑', id: 30 },
      ]
    },
    {
      title: '健财',
      classType: 40,
      sub: [
        { name: '健财', id: 40 },
      ]
    },
  ]

  constructor(
    public navCtrl: NavController,
    public api: Api,
    public navParams: NavParams
  ) {
    super()
  }

	/**
   * 下拉刷新
   * @param refresher 
   */
  doRefresh(refresher) {
    setTimeout(() => {
      this.getStudyList();
      refresher.complete();
    }, 2000);
  }

  pageLoad() {
    this.getStudyList()
    this.getCurriculumType()
  }

  /**
   * 获取课程列表
   * @param pIndex 
   */
  getStudyList(pIndex = 1) {
    // V1 /
    // this.studyList = StudyListData
    // http://mylaizhan.com:8082/V1/GetHotCurriculumlist?_=1518104261397
    this.api.postBody('V1/GetCurriculumByState', {
      // SourceType:1,
      PageIndex: pIndex,
      PageSize: this.pSize,
      Title: this.keywordInput,
      Type: this.typeActive,
    })
      .then(res => {
        if (pIndex === 1) {
          this.studyList = res.Data;
        }
        else {
          this.studyList = this.studyList.concat(res.Data);
        }
      })
      .catch(err => {
        console.log(err)
      })
  }

  // 获取课程分类
  getCurriculumType() {
    this.api.postBody('V1/GetCurriculumType', {
    })
      .then(res => {
        console.log(res)
        // this.formatCurriculumType(res.Data)
      })
      .catch(err => {

      })
  }

  // 格式化分类
  formatCurriculumType(typeList) {
    let _typeObj = {
      '10': [],
      '20': [],
      '30': [],
      '40': []
    }
    typeList.map(item => {
      _typeObj[item.ClassType].push(item)
    })
    this.classTypeConfig = [

    ]
  }

  toggleClassTypeListModal() {
    this.showClassTypeFlag = !this.showClassTypeFlag;
  }

  showClassTypeFlag = false;
  showClassTypeListModal() {
    this.showClassTypeFlag = true;
  }

  hideClassTypeListModal() {
    this.showClassTypeFlag = false;
  }

  expandClassType(classTypeItem) {
    // this.classTypeConfig.forEach(item => {
    //   item.isExpand = false
    // })
    // console.log(classTypeItem)
    classTypeItem.isExpand = !classTypeItem.isExpand
  }

  // 选择分类
  selectClassTypeItem(classTypeId) {
    if (this.classTypeId === classTypeId) {
      return
    }
    this.classTypeId = classTypeId
    this.hideClassTypeListModal()

    console.log('开始搜索' + this.classTypeId)
  }

  tabStudyType(type) {
    if (type === this.typeActive) {
      return
    }
    this.typeActive = type
    this.getStudyList(1)
    console.log('开始请求' + type)
  }

  clearSearchInput() {
    this.keywordInput = ''
  }

  searchNow(ev) {
    if (ev.keyCode == 13) {
      this.getStudyList(1);
      console.log('开始请求' + this.keywordInput)
    }
  }

  goStudyDetail(lessonId) {
    this.navCtrl.push(StudyDetailPage, {
      id: lessonId
    })
  }

  // setPageInfo(index) {
  //   this.api.postBody("V1/GetHotCurriculumlist", {
  //     "SourceType": this.typeActive,
  //     "PageIndex": index,
  //     "PageSize": 10
  //   })
  //     .then(data => {
  //       if (data.Code == 0) {
  //         this.xuexi_list = data.Data.Items;
  //       }
  //     })
  // }


}